package com.javaweb.admin.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.javaweb.admin.MenuEle;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.system.entity.Menu;
import com.javaweb.system.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private MenuMapper menuMapper;

    @GetMapping("/index")
    public JsonResult index() {
        File file = new File("E:\\1.txt");
        String content = txt2String(file);
//        Pattern p = Pattern.compile("\\s*|\t|\r|\n");
//        Matcher m = p.matcher(content);
//        content = m.replaceAll("");
        JSONObject jsStr = JSONObject.parseObject(content);

        JSONArray jsonArray = JSONArray.parseArray(jsStr.getString("data"));
        List<MenuEle> menuList = jsonArray.toJavaList(MenuEle.class);
        if (!menuList.isEmpty()) {
            for (MenuEle menuEle : menuList) {
                Menu menu = new Menu();
                menu.setPid(menuEle.getParentId());
                menu.setTitle(menuEle.getTitle());
                menu.setIcon(menuEle.getIcon());
                menu.setPath(menuEle.getPath());
                menu.setComponent(menuEle.getComponent());
                menu.setTarget(menuEle.getTarget());
                menu.setPermission(menuEle.getAuthority());
                menu.setType(menuEle.getMenuType());
                menu.setStatus(1);
                menu.setSort(menuEle.getSortNumber());
                menu.setCreateUser(1);
                menu.setCreateTime(DateUtils.now());
                menu.setUpdateUser(1);
                menu.setUpdateTime(DateUtils.now());
                menuMapper.insert(menu);

                // 子级
                if (!menuEle.getChildren().isEmpty()) {
                    for (MenuEle child : menuEle.getChildren()) {
                        Menu menu1 = new Menu();
                        menu1.setPid(menu.getId());
                        menu1.setTitle(child.getTitle());
                        menu1.setIcon(child.getIcon());
                        menu1.setPath(child.getPath());
                        menu1.setComponent(child.getComponent());
                        menu1.setTarget(child.getTarget());
                        menu1.setPermission(child.getAuthority());
                        menu1.setType(child.getMenuType());
                        menu1.setStatus(1);
                        menu1.setSort(child.getSortNumber());
                        menu1.setCreateUser(1);
                        menu1.setCreateTime(DateUtils.now());
                        menu1.setUpdateUser(1);
                        menu1.setUpdateTime(DateUtils.now());
                        menuMapper.insert(menu1);
                    }
                }
            }
        }

        System.out.println(content);

        return JsonResult.success();
    }

    public static String txt2String(File file) {
        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                result.append(System.lineSeparator() + s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

}
